//
//  SettingCouponViewController.m
//  Findif
//
//  Created by Mac on 2018/11/12.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "SettingCouponViewController.h"
#import "PGDatePickManager.h"

@interface SettingCouponViewController ()<PGDatePickerDelegate,UITextFieldDelegate>

@property (nonatomic, weak)UIView * backView;
//@property (nonatomic, strong)UILabel * styleLabel;
@property (nonatomic, strong)UILabel * modeLabel;
@property (nonatomic, strong)UILabel * countLabel;
@property (nonatomic, strong)UILabel * moneyLabel;
@property (nonatomic, strong)UILabel * conditionLabel;
@property (nonatomic, strong)UILabel * dateLabel;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * manLabel;
@property (nonatomic, strong)UILabel * useLabel;
@property (nonatomic, strong)UILabel * overdueLabel;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UIView * fiveLineView;
@property (nonatomic, strong)UIView * sixLineView;
@property (nonatomic, strong)UIView * sevenLineView;
@property (nonatomic, strong)UIView * eightLineView;
@property (nonatomic, weak)UIButton * sureButton;
//@property (nonatomic, strong)UIButton * allButton;
//@property (nonatomic, strong)UIButton * eatButton;
//@property (nonatomic, strong)UIButton * playButton;
//@property (nonatomic, strong)UIButton * sportButton;
//@property (nonatomic, strong)UIButton * travelButton;
@property (nonatomic, strong)UIButton * manButton;
@property (nonatomic, strong)UIButton * kouButton;
@property (nonatomic, strong)UITextField * nameTextField;
@property (nonatomic, strong)UITextField * moneyTextField;
@property (nonatomic, strong)UITextField * countTextField;
@property (nonatomic, strong)UITextField * conditionTextField;
@property (nonatomic, strong)UITextField * dayTextField;
@property (nonatomic, strong)UITextField * overTextField;
@property (nonatomic, strong)UIImageView * startImageView;
@property (nonatomic, strong)UIImageView * endImageView;
@property (nonatomic, strong)UIButton * startButton;
@property (nonatomic, strong)UIButton * endButton;

@property (nonatomic, strong)UILabel * ziLabel;
@property (nonatomic, strong)UILabel * tianLabel;
@property (nonatomic, strong)UILabel * overZheLabel;
@property (nonatomic, strong)UILabel * yuanLabel;
@property (nonatomic, strong)UIView * oneTextLineView;
@property (nonatomic, strong)UIView * twoTextLineView;
@property (nonatomic, strong)UIView * threeTextLineView;

@property (nonatomic, copy)NSString * type;
@property (nonatomic, copy)NSString * style;
@property (nonatomic, copy)NSString * mode;

@end

@implementation SettingCouponViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xEFF0F1);
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    if ([self.titleType isEqualToString:@"edit"]) {
        
        self.title = @"编辑优惠券";
    }else{
        
        self.title = @"定义优惠券";
    }
    
    
    //    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
    //
    //    self.navigationItem.backBarButtonItem = backItem;
}


- (void)setupUI{
    
    self.style = @"0";
    self.mode = @"1";
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    //    self.styleLabel = [self setupLabelWithTitle:@"优惠券"];
    //    [self.backView addSubview:self.styleLabel];
    self.modeLabel = [self setupLabelWithTitle:@"优惠模式"];
    [self.backView addSubview:self.modeLabel];
    self.countLabel = [self setupLabelWithTitle:@"优惠券个数"];
    [self.backView addSubview:self.countLabel];
    self.moneyLabel = [self setupLabelWithTitle:@"优惠金额"];
    [self.backView addSubview:self.moneyLabel];
    self.conditionLabel = [self setupLabelWithTitle:@"使用条件"];
    [self.backView addSubview:self.conditionLabel];
    self.dateLabel = [self setupLabelWithTitle:@"使用期限"];
    [self.backView addSubview:self.dateLabel];
    self.nameLabel = [self setupLabelWithTitle:@"优惠券名称"];
    [self.backView addSubview:self.nameLabel];
    self.overdueLabel = [self setupLabelWithTitle:@"作废时间"];
    [self.backView addSubview:self.overdueLabel];
    
    self.oneLineView = [self setupLineView];
    [self.backView addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self.backView addSubview:self.twoLineView];
    self.threeLineView = [self setupLineView];
    [self.backView addSubview:self.threeLineView];
    self.fourLineView = [self setupLineView];
    [self.backView addSubview:self.fourLineView];
    self.fiveLineView = [self setupLineView];
    [self.backView addSubview:self.fiveLineView];
    self.sixLineView = [self setupLineView];
    [self.backView addSubview:self.sixLineView];
    self.sevenLineView = [self setupLineView];
    [self.backView addSubview:self.sevenLineView];
    self.eightLineView = [self setupLineView];
    [self.backView addSubview:self.eightLineView];
    self.oneTextLineView = [self setupLineView];
    [self.backView addSubview:self.oneTextLineView];
    self.twoTextLineView = [self setupLineView];
    [self.backView addSubview:self.twoTextLineView];
    self.threeTextLineView = [self setupLineView];
    [self.backView addSubview:self.threeTextLineView];
    //    self.allButton = [self setupButtonWithTitle:@" 通用"];
    //    self.allButton.selected = YES;
    //    [self.backView addSubview:self.allButton];
    //    self.eatButton = [self setupButtonWithTitle:@" 吃喝"];
    //    [self.backView addSubview:self.eatButton];
    //    self.playButton = [self setupButtonWithTitle:@" 玩乐"];
    //    [self.backView addSubview:self.playButton];
    //    self.sportButton = [self setupButtonWithTitle:@" 运动"];
    //    [self.backView addSubview:self.sportButton];
    //    self.travelButton = [self setupButtonWithTitle:@" 旅行"];
    //    [self.backView addSubview:self.travelButton];
    self.manButton = [self setupButtonWithTitle:@" 满减优惠"];
    self.manButton.selected = YES;
    [self.backView addSubview:self.manButton];
    self.kouButton = [self setupButtonWithTitle:@" 折扣优惠"];
    [self.backView addSubview:self.kouButton];
    
    
    self.moneyTextField = [self setupTextFieldWithTitle:@"请输入优惠金额"];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10 * SCREENH6, 10 * SCREENH6)];
    label.text = @"￥";
    label.textColor = ZRBlackColor;
    label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    self.moneyTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 13 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    [self.moneyTextField.leftView addSubview:label];
    label.centerY = self.moneyTextField.leftView.centerY;
    self.moneyTextField.leftViewMode = UITextFieldViewModeAlways;
    
    [self.backView addSubview:self.moneyTextField];
    
    self.nameTextField = [self setupTextFieldWithTitle:@"请输入优惠券名称"];
    self.nameTextField.keyboardType = UIKeyboardTypeDefault;
    [self.backView addSubview:self.nameTextField];
    self.countTextField = [self setupTextFieldWithTitle:@"请输入优惠券数量"];
    [self.backView addSubview:self.countTextField];
    self.conditionTextField = [self setupTextFieldWithTitle:@"如:100"];
    self.conditionTextField.textAlignment = NSTextAlignmentCenter;
    [self.backView addSubview:self.conditionTextField];
    self.dayTextField = [self setupTextFieldWithTitle:@"如:2"];
    self.dayTextField.textAlignment = NSTextAlignmentCenter;
    [self.backView addSubview:self.dayTextField];
    self.overTextField = [self setupTextFieldWithTitle:@"如:100"];
    self.overTextField.textAlignment = NSTextAlignmentCenter;
    [self.backView addSubview:self.overTextField];
    
    self.manLabel = [self setupGaryLabelWithTitle:@"满"];
    [self.backView addSubview:self.manLabel];
    self.useLabel = [self setupGaryLabelWithTitle:@"使用"];
    [self.backView addSubview:self.useLabel];
    
    self.ziLabel = [self setupGaryLabelWithTitle:@"自领取"];
    [self.backView addSubview:self.ziLabel];
    self.tianLabel = [self setupGaryLabelWithTitle:@"天之后"];
    [self.backView addSubview:self.tianLabel];
    
    self.overZheLabel = [self setupGaryLabelWithTitle:@"折扣上限"];
    [self.backView addSubview:self.overZheLabel];
    self.yuanLabel = [self setupGaryLabelWithTitle:@"元"];
    [self.backView addSubview:self.yuanLabel];
    
    self.endImageView = [self setupImageView];
    [self.backView addSubview:self.endImageView];
    self.startImageView = [self setupImageView];
    [self.backView addSubview:self.startImageView];
    
    self.startButton = [self setupDateButtonWithTitle:@"选择发放时间"];
    [self.backView addSubview:self.startButton];
    self.endButton = [self setupDateButtonWithTitle:@"选择结束时间"];
    [self.backView addSubview:self.endButton];
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [sureButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"提交审核"] forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"提交审核_灰"] forState:UIControlStateDisabled];
    [sureButton addTarget:self action:@selector(clickSureButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    sureButton.enabled = NO;
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
    
    self.overZheLabel.hidden = YES;
    self.overTextField.hidden = YES;
    self.yuanLabel.hidden = YES;
    self.twoTextLineView.hidden = YES;
    
    if (self.model) {
        
        //        int usable_goods_type = [self.model.usable_goods_type intValue];
        //        if (usable_goods_type == 0) {
        //
        //            self.style = @"0";
        //
        //            self.allButton.selected = YES;
        //            self.normalButton.selected = NO;
        //            self.tuanButton.selected = NO;
        //        }else if (usable_goods_type == 1){
        //
        //            self.style = @"1";
        //
        //            self.allButton.selected = NO;
        //            self.normalButton.selected = YES;
        //            self.tuanButton.selected = NO;
        //        }else if (usable_goods_type == 2){
        //
        //            self.style = @"2";
        //
        //            self.allButton.selected = NO;
        //            self.normalButton.selected = NO;
        //            self.tuanButton.selected = YES;
        //        }
        
        int type = [self.model.type intValue];
        
        if (type == 1) {
            
            self.mode = @"1";
            
            self.manButton.selected = YES;
            self.kouButton.selected = NO;
            
            self.moneyLabel.text = @"优惠金额";
            self.moneyTextField.leftViewMode = UITextFieldViewModeAlways;
            self.moneyTextField.placeholder = @"请输入优惠金额";
            
            self.overZheLabel.hidden = YES;
            self.overTextField.hidden = YES;
            self.yuanLabel.hidden = YES;
            self.twoTextLineView.hidden = YES;
            
        }else if (type == 2){
            
            self.mode = @"2";
            
            self.kouButton.selected = YES;
            self.manButton.selected = NO;
            
            self.moneyLabel.text = @"优惠折扣";
            self.moneyTextField.leftViewMode = UITextFieldViewModeNever;
            self.moneyTextField.placeholder = @"请输入优惠折扣";
            
            self.overZheLabel.hidden = NO;
            self.overTextField.hidden = NO;
            self.yuanLabel.hidden = NO;
            self.twoTextLineView.hidden = NO;
            
            self.overTextField.text = [NSString stringWithFormat:@"%@",self.model.upper_limit_money];
            self.dayTextField.text = [NSString stringWithFormat:@"%@",self.model.aging_time];
        }
        
        self.nameTextField.text = self.model.coupons_name;
        self.moneyTextField.text = self.model.discount;
        self.countTextField.text = self.model.issue_number;
        self.conditionTextField.text = self.model.use_standard;
        self.startButton.selected = YES;
        self.endButton.selected = YES;
        [self.startButton setTitle:self.model.starttime forState:UIControlStateNormal];
        [self.endButton setTitle:self.model.overtime forState:UIControlStateNormal];
        
        [self changButtonState];
    }
}


- (void)viewDidLayoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(350 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6 + kNavHeight + kStatusBarHeight);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backView).offset(50 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(18 * SCREENH6);
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(95 * SCREENH6);
        make.trailing.mas_equalTo(self.oneLineView);
        make.centerY.mas_equalTo(self.nameLabel);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    //    [self.styleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.leading.mas_equalTo(self.nameLabel);
    //        make.top.mas_equalTo(self.self.oneLineView.mas_bottom).offset(18 * SCREENH6);
    //    }];
    //
    //    [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.leading.mas_equalTo(self.nameTextField);
    //        make.centerY.mas_equalTo(self.styleLabel);
    ////        make.width.mas_equalTo(50 * SCREENH6);
    ////        make.height.mas_equalTo(30 * SCREENH6);
    //    }];
    //
    //    [self.eatButton mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.mas_equalTo(self.allButton);
    //        make.leading.mas_equalTo(self.allButton.mas_trailing).offset(8 * SCREENH6);
    //    }];
    //
    //    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.mas_equalTo(self.eatButton);
    //        make.leading.mas_equalTo(self.eatButton.mas_trailing).offset(8 * SCREENH6);
    //    }];
    //
    //    [self.sportButton mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.mas_equalTo(self.playButton);
    //        make.leading.mas_equalTo(self.playButton.mas_trailing).offset(8 * SCREENH6);
    //    }];
    //
    //    [self.travelButton mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.mas_equalTo(self.sportButton);
    //        make.leading.mas_equalTo(self.sportButton.mas_trailing).offset(8 * SCREENH6);
    //    }];
    //
    //    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.leading.trailing.height.mas_equalTo(self.oneLineView);
    //        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(50 * SCREENH6);
    //    }];
    
    [self.modeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.manButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameTextField);
        make.centerY.mas_equalTo(self.modeLabel);
    }];
    
    [self.kouButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.manButton);
        make.leading.mas_equalTo(self.manButton.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(50 * SCREENH6);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.threeLineView);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.moneyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameTextField);
        make.centerY.mas_equalTo(self.moneyLabel);
    }];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.threeLineView);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(50 * SCREENH6);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.fourLineView);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.countTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.moneyTextField);
        make.centerY.mas_equalTo(self.countLabel);
    }];
    
    [self.fiveLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.fourLineView);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(50 * SCREENH6);
    }];
    
    [self.conditionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.fiveLineView);
        make.top.mas_equalTo(self.fiveLineView.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.manLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.countTextField);
        make.centerY.mas_equalTo(self.conditionLabel);
    }];
    
    [self.conditionTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.manLabel);
        make.leading.mas_equalTo(self.manLabel.mas_trailing).offset(5 * SCREENH6);
        make.height.mas_equalTo(self.moneyTextField);
        make.width.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.oneTextLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.conditionTextField);
        make.top.mas_equalTo(self.conditionTextField.mas_bottom);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.useLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.conditionTextField);
        make.leading.mas_equalTo(self.conditionTextField.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.overZheLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.useLabel);
        make.leading.mas_equalTo(self.useLabel.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.overTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.overZheLabel);
        make.leading.mas_equalTo(self.overZheLabel.mas_trailing);
        make.height.mas_equalTo(self.conditionTextField);
        make.width.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.twoTextLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.overTextField);
        make.top.mas_equalTo(self.overTextField.mas_bottom);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.yuanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.overTextField);
        make.leading.mas_equalTo(self.overTextField.mas_trailing);
    }];
    
    [self.sixLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.fiveLineView);
        make.top.mas_equalTo(self.fiveLineView.mas_bottom).offset(50 * SCREENH6);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.sixLineView);
        make.top.mas_equalTo(self.sixLineView.mas_bottom).offset(18 * SCREENH6);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.manLabel);
        make.centerY.mas_equalTo(self.dateLabel);
        make.height.mas_equalTo(self.conditionTextField);
        make.width.mas_equalTo(85 * SCREENH6);
    }];
    
    [self.startImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.startButton.mas_trailing).offset(3 * SCREENH6);
        make.centerY.mas_equalTo(self.startButton);
        make.width.mas_equalTo(9 * SCREENH6);
        make.height.mas_equalTo(6 * SCREENH6);
    }];
    
    [self.sevenLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1 * SCREENH6);
        make.leading.mas_equalTo(self.startImageView.mas_trailing).offset(15 * SCREENH6);
        make.centerY.mas_equalTo(self.startImageView);
        make.height.mas_equalTo(35 * SCREENH6);
    }];
    
    [self.endButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.sevenLineView.mas_trailing).offset(15 * SCREENH6);
        make.centerY.height.width.mas_equalTo(self.startButton);
    }];
    
    [self.endImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.endButton.mas_trailing).offset(3 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.startImageView);
    }];
    
    [self.eightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.sixLineView);
        make.top.mas_equalTo(self.sixLineView.mas_bottom).offset(50 * SCREENH6);
    }];
    
    [self.overdueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel);
        make.top.mas_equalTo(self.eightLineView.mas_bottom).offset(16 * SCREENH6);
    }];
    
    [self.ziLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.startButton);
        make.centerY.mas_equalTo(self.overdueLabel);
    }];
    
    [self.dayTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.ziLabel);
        make.leading.mas_equalTo(self.ziLabel.mas_trailing).offset(5 * SCREENH6);
        make.height.mas_equalTo(self.conditionTextField);
        make.width.mas_equalTo(80 * SCREENH6);
    }];
    
    [self.threeTextLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.dayTextField);
        make.top.mas_equalTo(self.dayTextField.mas_bottom);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.tianLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dayTextField.mas_trailing);
        make.centerY.mas_equalTo(self.dayTextField);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backView.mas_bottom).offset(30 * SCREENH6);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(258 * SCREENH6);
        make.height.mas_equalTo(38 * SCREENH6);
    }];
}


- (UITextField *)setupTextFieldWithTitle:(NSString *)title{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = ZRWhiteColor;
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:title attributes:attrs];
    textFiled.textColor = ZRBlackColor;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
    textFiled.keyboardType = UIKeyboardTypeDecimalPad;
    textFiled.delegate = self;
    
    return textFiled;
}


- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    button.titleLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0x252525) forState:UIControlStateSelected];
    [button setTitleColor:ColorWithRGB(0x909090) forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(clickStyleOrModeButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (void)presentDateController{
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerType1;
    datePicker.isHiddenMiddleText = false;
    datePicker.datePickerMode = PGDatePickerModeDate;
    //    datePicker.maximumDate = [NSDate date];
    datePicker.language = @"zh-Hans";
    [self presentViewController:datePickManager animated:false completion:nil];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    NSString * date = [NSString stringWithFormat:@"%li-%02li-%02li",(long)dateComponents.year,dateComponents.month,dateComponents.day];
    
    if ([self.type isEqualToString:@"start"]) {
        
        [self.startButton setTitle:date forState:UIControlStateNormal];
        self.startButton.selected = YES;
    }else{
        
        [self.endButton setTitle:date forState:UIControlStateNormal];
        self.endButton.selected = YES;
    }
    
    [self changButtonState];
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xE4E4E4);
    
    return lineView;
}


- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = ZRBlackColor;
    label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    label.text = title;
    
    return label;
}


- (UILabel *)setupGaryLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    label.text = title;
    
    return label;
}


- (UIImageView *)setupImageView{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:@"多边形1"];
    
    return imageView;
}

- (UIButton *)setupDateButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:ZRBlackColor forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    [button addTarget:self action:@selector(clickSetupDateViewButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (void)clickSetupDateViewButton:(UIButton *)button{
    
    if (button == self.startButton) {
        
        self.type = @"start";
        
        [self presentDateController];
    }else{
        
        self.type = @"end";
        
        [self presentDateController];
    }
}


- (void)clickStyleOrModeButton:(UIButton *)button{
    
    //    if (button == self.allButton) {
    //
    //        self.style = @"0";
    //        if (!button.selected) {
    //
    //            button.selected = YES;
    //
    //            self.eatButton.selected = NO;
    //            self.playButton.selected = NO;
    //            self.sportButton.selected = NO;
    //            self.travelButton.selected = NO;
    //        }
    //    }else if (button == self.eatButton){
    //
    //        self.style = @"1";
    //        if (!button.selected) {
    //
    //            button.selected = YES;
    //
    //            self.allButton.selected = NO;
    //            self.playButton.selected = NO;
    //            self.sportButton.selected = NO;
    //            self.travelButton.selected = NO;
    //        }
    //    }else if (button == self.playButton){
    //
    //        self.style = @"2";
    //        if (!button.selected) {
    //
    //            button.selected = YES;
    //
    //            self.allButton.selected = NO;
    //            self.eatButton.selected = NO;
    //            self.sportButton.selected = NO;
    //            self.travelButton.selected = NO;
    //        }
    //    }else if (button == self.sportButton){
    //
    //        self.style = @"3";
    //        if (!button.selected) {
    //
    //            button.selected = YES;
    //
    //            self.allButton.selected = NO;
    //            self.eatButton.selected = NO;
    //            self.playButton.selected = NO;
    //            self.travelButton.selected = NO;
    //        }
    //    }else if (button == self.travelButton){
    //
    //        self.style = @"4";
    //        if (!button.selected) {
    //
    //            button.selected = YES;
    //
    //            self.allButton.selected = NO;
    //            self.eatButton.selected = NO;
    //            self.sportButton.selected = NO;
    //            self.sportButton.selected = NO;
    //        }
    //    }else
    if (button == self.manButton){
        
        self.mode = @"1";
        if (!button.selected) {
            
            button.selected = YES;
            self.kouButton.selected = NO;
            
            self.moneyLabel.text = @"优惠金额";
            self.moneyTextField.leftViewMode = UITextFieldViewModeAlways;
            self.moneyTextField.placeholder = @"请输入优惠金额";
            self.overZheLabel.hidden = YES;
            self.overTextField.hidden = YES;
            self.yuanLabel.hidden = YES;
            self.twoTextLineView.hidden = YES;
        }
    }else if (button == self.kouButton){
        
        self.mode = @"2";
        if (!button.selected) {
            
            button.selected = YES;
            self.manButton.selected = NO;
            
            self.moneyLabel.text = @"优惠折扣";
            self.moneyTextField.leftViewMode = UITextFieldViewModeNever;
            self.moneyTextField.placeholder = @"请输入优惠折扣";
            self.overZheLabel.hidden = NO;
            self.overTextField.hidden = NO;
            self.yuanLabel.hidden = NO;
            self.twoTextLineView.hidden = NO;
        }
    }
    
}


- (void)clickSureButton{
    
    if (self.model) {
        
        NSString * ID = self.model.id;
        
        NSDictionary * dict = @{@"coupons_name":self.nameTextField.text,@"discount":self.moneyTextField.text,@"use_standard":self.conditionTextField.text,@"starttime":self.startButton.titleLabel.text,@"overtime":self.endButton.titleLabel.text,@"type":self.mode,@"usable_goods_type":@"2",@"issue_number":self.countTextField.text,@"id":ID};
        
        [[ZRNetworkTool sharedTool] editMerchantsCouponsResultWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你，修改成功，快去看看吧！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
        
    }else{
        
        NSDictionary * dict;
        if ([self.mode isEqualToString:@"1"]) {
            //满减
            dict = @{@"coupons_name":self.nameTextField.text,@"discount":self.moneyTextField.text,@"use_standard":self.conditionTextField.text,@"starttime":self.startButton.titleLabel.text,@"overtime":self.endButton.titleLabel.text,@"type":self.mode,@"usable_goods_type":@"1",@"issue_number":self.countTextField.text,@"aging_time":self.dayTextField.text};
        }else{
            //折扣
            dict = @{@"coupons_name":self.nameTextField.text,@"discount":self.moneyTextField.text,@"use_standard":self.conditionTextField.text,@"starttime":self.startButton.titleLabel.text,@"overtime":self.endButton.titleLabel.text,@"type":self.mode,@"usable_goods_type":@"1",@"issue_number":self.countTextField.text,@"aging_time":self.dayTextField.text,@"upper_limit_money":self.overTextField.text};
        }
        
        [[ZRNetworkTool sharedTool] addMerchantsCouponsResultWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你，发布成功，快去看看吧！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    [self changButtonState];
}

- (void)changButtonState{
    
    if ([self.mode isEqualToString:@"1"]) {
       
        if (self.moneyTextField.text.length > 0 && self.countTextField.text.length > 0 && self.nameTextField.text.length > 0 && self.conditionTextField.text.length > 0 && self.startButton.selected && self.endButton.selected && self.dayTextField.text.length > 0) {
            
            self.sureButton.enabled = YES;
        }else{
            
            self.sureButton.enabled = NO;
        }
    }else{
        
        if (self.moneyTextField.text.length > 0 && self.countTextField.text.length > 0 && self.nameTextField.text.length > 0 && self.conditionTextField.text.length > 0 && self.startButton.selected && self.endButton.selected && self.dayTextField.text.length > 0 && self.overTextField.text.length > 0) {
            
            self.sureButton.enabled = YES;
        }else{
            
            self.sureButton.enabled = NO;
        }
    }
    
    
    
}



@end
